// pages/posts/post-detail/post-detail.js
var postsData = require('../../../data/posts-data.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlayingMusic: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   var postId = options.id;
   this.data.currentPostId = postId;
   this.setData({
     postData: postsData.postList[postId]
   })
   
   var postsCollected = wx.getStorageSync('posts_collected')
   if(postsCollected) {
     var postCollected;
     postsCollected[postId]? postCollected = postsCollected[postId]:postCollected = false
     this.setData({
       collected: postCollected
     })
   } else {
     var postsCollected = {}
     postsCollected[postId] = false
     wx.setStorageSync('posts_collected', postsCollected)
   }
   if(app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId === postId){
     this.setData({
       isPlayingMusic: true
     })
    //  this.setMusicMonitor();
   }
  },
  setMusicMonitor: function(){
    var _this = this;
    wx.onBackgroundAudioPlay(function(event){
      var pages = getCurrentPages();
      console.log(pages);
      console.log(123)
      var currentPage = pages[pages.length -1];
      if(currentPage.data.currentPostId === _this.data.currentPsotId) {
        _this.setData({
          isPlayingMusic: true
        })
      }
      app.globalData.g_isPlayingMusic = true;
    })
    wx.onBackgroundAudioPause(function(event){
      var pages = getCurrentPages();
      var currentPage = pages[pages.length - 1];
      if(currentPage.data.currentPostId === _this.data.currentPostId){
        if(app.gobalData.g_currentMusicPostId === _this.data.currentPostId){
          _this.setData({
            isPlayingMusic: false
          })
        }
      }
      app.globalData.g_isPlayingMusic = false
    })

    wx.onBackgroundAudioStop(()=>{
      this.setData({
        isPlayingMusic: false
      })
      app.globalData.g_isPlayingMusic = false;
    })  

  },

  onMusicTap: function (event){
    var currentPostId = this.data.currentPostId;
    var postData = postsData.postList[currentPostId]
    var isPlayingMusic = this.data.isPlayingMusic
    if(isPlayingMusic) {
      wx.pauseBackgroundAudio();
      this.setData({
        isPlayingMusic: false
      })
      app.globalData.g_isPlayingMusic = false
      }else{
        wx.playBackgroundAudio({
          dataUrl: postData.music.url,
          title: postData.music.title,
          coverImgUrl: postData.music.coverImg
        })
        this.setData({
          isPlayingMusic: true
        })
        app.globalData.g_currentMusicPostId = this.data.currentPostId;
        app.globalData.g_isPlayingMusic = true
      }
  },
  onCollectionTap () {
    wx.getStorage({
      key: 'posts_collected',
      success: res=>{
        var postsCollected = res.data;
        var postCollected = postsCollected[this.data.currentPostId]
        postCollected = !postCollected;
        postsCollected[this.data.currentPostId] = postCollected;
        wx.setStorageSync('posts_collected', postsCollected)
        this.setData({
          collected: postCollected
        })
        wx.showToast({
          title: postCollected?'收藏成功':'取消成功',
          duration: 1000,
          icon: 'success'
        })
      }
    })
  },
  onShareTap () {
    const itemList = [
      "分享微信好友",
      '分享到朋友圈'
    ]
    wx.showActionSheet({
      itemList: itemList,
      itemColor: '#405f80',
      success: (res)=>{
        wx.showModal({
          title: '用户'+itemList[res.tapIndex],
          content: '哟令狐'+res.cancel,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const pages = getCurrentPages()
    console.log(pages[pages.length-1])
    const path = '/pages/posts/post-detail/post-detail?id='+this.data.currentPostId
    console.log(path)
    return {
      title: '自定义转发标题',
      path: path,
      success: function (res) {
        // 转发成功
        console.log(res)
      },
      fail: function (res) {
        // 转发失败
        console.log(res)
      }
    }
  }
})